package com.office.automate.encrypt.password;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.exception.general.GeneralException;

public class EncryptPassword {

	public String getEncryptedPasswordMD5(String aPassword) throws FrontierVisibleException {

		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(aPassword.getBytes());
			byte byteData[] = messageDigest.digest();

	        //convert the byte to hex format
	        StringBuffer sb = new StringBuffer();
	        for (int i = 0; i < byteData.length; i++) {
	         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
	        }
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_PASSWORD_ENCRYPTION, e);
		}
	}
}